package com.johnny.xia.util.Java8;

import com.alibaba.fastjson.JSON;
import com.johnny.xia.util.base.RandUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName StreamTester
 * @Author johnny xia
 * @mailto johnny.xia@qq.com
 * @Date 2020/10/19 6:06 下午
 * @Description TODO
 **/
public class StreamTester {

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> after = strings.stream().filter(test->test.startsWith("abc")).collect(Collectors.toList());
        System.out.println(JSON.toJSONString(after));

        List<Member> members = new ArrayList<>();
        for (int i=1;i<20;i++) {
            Member member = new Member();
            member.age= RandUtil.RandInt(10,30);
            member.gender = RandUtil.RandInt(0,2)%2 == 0 ? "男" : "女";
            member.name = "name"+i;
            members.add(member);
        }

        List<Member> young = members.stream().filter(member->member.getAge()<18).collect(Collectors.toList());

        System.out.println(JSON.toJSONString(young));

        members.forEach(Member::say);


        List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        // 获取对应的平方数
        List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());

        System.out.printf(JSON.toJSONString(squaresList));
    }

    @Data
    public static class Member{

        private String name;

        private int age;

        private String gender;

        public void say(){
            System.out.println("hello,"+this.toString());
        }
    }
}
